/*
# Rouleur Log - A log for cyclists.
# Copyright (C) 2006  Edward Copony (ecopony@gmail.com)
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package com.blazingredfireball.rouleurlog.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.blazingredfireball.rouleurlog.business.BikeDelegate;
	import com.blazingredfireball.rouleurlog.business.SQLStatementConfiguration;
	import com.blazingredfireball.rouleurlog.model.RouleurlogModelLocator;
	import com.blazingredfireball.rouleurlog.vo.BikeVO;
	import com.ericfeminella.sql.ISQLResponder;
	import com.ericfeminella.sql.SQLStatementHelper;
	
	import flash.data.SQLResult;
	import flash.errors.SQLError;
	
	import mx.controls.Alert;

	public final class GetBikesCommand extends BaseCommand implements ICommand, ISQLResponder
	{
		[Bindable]
		private var model:RouleurlogModelLocator = RouleurlogModelLocator.getInstance();
		
		public function execute(event:CairngormEvent):void
		{
		  var delegate:BikeDelegate = new BikeDelegate(this);    

		  delegate.getBikes( SQLStatementHelper.create(SQLStatementConfiguration.SELECT_BIKES), BikeVO);
		}
		
		/**
		 * Don't feel good about having knowledge of the result set here. Check 
		 * out AIRCairngorm's approach for wrapping up that knowledge.
		 * 
		 */
		public function result(evt:SQLResult):void
		{
			model.resultMessage = "OPERATION COMPLETED";
			
			model.bikes.source = null;
			
			//Check for an empty result
			if (evt.data == null) return;
			
			var i:int = 0;
			var n:int = evt.data.length;
			
			for (i; i < n; i++) {
				var bike:BikeVO = evt.data[i];
				model.bikes.addItem( bike );
			}
		}
		
		public function fault(error:SQLError):void
		{
		  if (model.databaseInitialized) Alert.show("Bikes could not be retrieved!");
		}
	}
}